package com.tingyun.sign.core.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.tingyun.sign.core.entity.ScUManagerBean;

@Repository("scUManagerDao")
public interface ScUManagerDao extends PagingAndSortingRepository<ScUManagerBean, Integer>, JpaSpecificationExecutor<ScUManagerBean> {

	/**
	 * 根据管理员名称查询，判断用户名是否存在
	 * @param managerName
	 * @return
	 */
	@Query(value = "select count(id) from ScUManagerBean t where t.managerName = :managerName")
	public int findByName(@Param("managerName") String managerName);
	/**
	 * 检测时候存在超级管理员
	 * @param managerName
	 * @return
	 */
	@Query(value = "select count(type) from ScUManagerBean t where t.type = 0")
	public int checkByType();

	/**
	 * 根据管理员名称和密码查询，判断用户名信息是否正确
	 * @param managerName
	 * @param passWord
	 * @return
	 */
	@Query(value = "select t from ScUManagerBean t where t.managerName = :managerName and t.passWord = :pwd")
	public ScUManagerBean findByNameAndPwd(@Param("managerName") String managerName, @Param("pwd") String passWord);
	
	/**
	 * 编辑管理员账户
	 * @param managerName
	 * @param passWord
	 * @param status
	 * @Param type
	 * @Param muser
	 * @return
	 */
	@Modifying
	@Query(value = "update ScUManagerBean t set t.passWord=:passWord,t.mtime=sysdate ,t.muser=:muser, t.type= :type, t.status= :status where t.managerName = :managerName")
	public Integer editManager(@Param("managerName")String managerName,@Param("status")Integer status,@Param("type")Integer type,@Param("muser")Long muser,@Param("passWord")String passWord);
	
	/**
	 * 管理员列表
	 * @return
	 */
	@Query(value = "select t from ScUManagerBean t  order by id desc")
	public List<ScUManagerBean> findAllManagers();
	/**
	 * 删除管理员
	 * @param id
	 * @param muser
	 */
	@Modifying
	@Query(value = "update ScUManagerBean t set t.status = 1,t.muser = :muser,t.mtime=sysdate where t.id=:id and t.status=0")
	public void deleteById(@Param("id")Long id, @Param("muser")Long muser);
}